package cc.sheji.Service;


import cc.sheji.config.StaticConfig;
import cc.sheji.domain.Order;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.Map;

@Service
public class PayService {

    @Autowired
    OrderService orderService;


    @Autowired
    StaticConfig staticConfig;


    public void stockCouponsReceiveCallback(String requestBody,
                                            String wechatpaySerial,
                                            String wechatpaySignature,
                                            String wechatpayTimestamp,
                                            String wechatpayNonce) {


        // 构造 RequestParam
        com.wechat.pay.java.core.notification.RequestParam requestParam = new com.wechat.pay.java.core.notification.RequestParam.Builder()
                .serialNumber(wechatpaySerial)
                .nonce(wechatpayNonce)
                .signature(wechatpaySignature)
                .timestamp(wechatpayTimestamp)
                .body(requestBody)
                .build();
//// 如果已经初始化了 RSAAutoCertificateConfig，可直接使用
//// 没有的话，则构造一个

// 初始化 NotificationParser
        NotificationParser parser = new NotificationParser((NotificationConfig) staticConfig.getConfig());
// 以支付通知回调为例，验签、解密并转换成 Transaction
        Transaction transaction = parser.parse(requestParam, Transaction.class);
        String outTradeNo = transaction.getOutTradeNo();
        System.out.println("outTradeNO:" + outTradeNo);
        try {
            Long id = Long.parseLong(outTradeNo.replace("out_trade_no",""));
            orderService.updatePayStateFinish(id);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
